var httpConf = require("../../../conf/httpModule.js")
var conf = require("../../../conf/niuniuConf.js").niuConf
var tips = require("../../../conf/tips.js").tipsConf
var utils = require("../../../util/util.js")
var async = require("async")
module.exports = function(app) {
	return new DBRemote(app);
};

var DBRemote = function(app) {
	this.app = app
	DBRemote.app = app
    DBRemote.dbService = this.app.get("dbService")
    if(DBRemote.dbService && DBRemote.dbService.db){
    	DBRemote.db = DBRemote.dbService.db
    }
}

var remote = DBRemote.prototype
var local = {}
var teaUserMap = {}
var userTeaMap = {}

//房间结束回调
remote.gameoverCB = function(roomId,players,consumeFlag,cb) {
	console.log("gameoverCB")
	console.log(players)
	roomId = parseInt(roomId)
	var teaId = parseInt(roomId / 100)
	var tableId = parseInt(roomId % 100)
	var date = utils.getDateString()
	console.log("consumeFlag : ")
	console.log(consumeFlag)
	if(consumeFlag){
		//排行榜信息
		var maxScore = 0
		var maxUid = 0
		for(var i in players){
			if(players[i].isActive){
				if(players[i].score > maxScore){
					maxScore = players[i].score
					maxUid = players[i].uid
				}
				var info = {
					"uid" : players[i].uid,
					"nickname" : players[i].playerInfo.nickname,
					"head" : players[i].playerInfo.head
				}
				//更新玩家信息
				DBRemote.dbService.db.hset("teaHouseUserInfo:"+teaId,players[i].uid,JSON.stringify(info))
				//更新当日玩家分数
				DBRemote.dbService.db.zincrby("teaHouseScores:"+teaId+":"+date,players[i].score,players[i].uid)
				//更新总分数
				DBRemote.dbService.db.zincrby("teaHouseScores:"+teaId+":all",players[i].score,players[i].uid)
				//更新当日参玩次数
				DBRemote.dbService.db.zincrby("teaHousePlayCount:"+teaId+":"+date,1,players[i].uid)
				//更新总参玩次数
				DBRemote.dbService.db.zincrby("teaHousePlayCount:"+teaId+":all",1,players[i].uid)
			}
		}
		//更新当日大赢家次数
		DBRemote.dbService.db.zincrby("teaHouseWinCount:"+teaId+":"+date,1,maxUid)
		//更新总大赢家次数
		DBRemote.dbService.db.zincrby("teaHouseWinCount:"+teaId+":all",1,maxUid)
		//更新排行榜存储日期
		DBRemote.dbService.db.hset("teaHouseRankingDate:"+teaId,date,true,function(err) {
			//过期数据删除
			DBRemote.dbService.db.hgetall("teaHouseRankingDate:"+teaId,function(err,data) {
				if(data){
					for(var i in data){
						if(parseInt(i) < parseInt(date) - 100){
							DBRemote.dbService.db.hdel("teaHouseRankingDate:"+teaId,i)
							DBRemote.dbService.db.del("teaHouseScores:"+teaId+":"+i)
							DBRemote.dbService.db.del("teaHousePlayCount:"+teaId+":"+i)
							DBRemote.dbService.db.del("teaHouseWinCount:"+teaId+":"+i)
							for(var j = 1;j <= 8;j++){
								DBRemote.dbService.db.hdel("teaHouseDiamod:"+teaId,i+":"+j)
								DBRemote.dbService.db.hdel("teaHouseTableCount:"+teaId,i+":"+j)
							}
						}
					}
				}
			})
		})
	}
	//通知
	var notify = {
		"code" : "tableOver",
		"tableId" : tableId,
	}
	local.sendTea(teaId,notify)
	cb()
}
//设置茶楼战绩
remote.setTeaHistory = function(teaId,record,cb) {
	var maxIndex = 20
	DBRemote.dbService.db.rpush("teaHouseRecord:"+teaId,JSON.stringify(record),function(err,data) {
		console.log("rpush")
		console.log(data)
		if(data > maxIndex){
			DBRemote.dbService.db.ltrim("teaHouseRecord:"+teaId,-maxIndex,-1)
		}
	})
	cb()
}

//获取茶楼战绩
remote.getTeaRecord = function(uid,begin,end,cb) {
	if(!begin | typeof(begin) !== "number" || begin < 0 || begin > 100){
		begin = 10
	}
	if(!end | typeof(end) !== "number" || end < 0 || end > 100){
		end = 1
	}
	var teaId = userTeaMap[uid]
	if(!teaId){
		console.log("未加入茶楼")
		DBRemote.app.rpc.game.remote.sendTipsByUid(null,uid,"请先加入一个茶楼",function(){})
		cb(false)
		return
	}
	DBRemote.dbService.db.lrange("teaHouseRecord:"+teaId,-end,-begin,function(err,data) {
		cb(data)
	})
}
//获取茶楼分数排行榜
remote.getTeaRankingList = function(uid,type,code,cb) {
	var teaId = userTeaMap[uid]
	if(!teaId){
		DBRemote.app.rpc.game.remote.sendTipsByUid(null,uid,"请先加入一个茶楼",function(){})
		cb(false)
		return
	}
	var str = ""
	switch(type){
		case "ranking" :
			str = "teaHouseScores:"
		break
		case "playCount" :
			str = "teaHousePlayCount:"
		break
		case "winCount" :
			str = "teaHouseWinCount:"
		break
		default :
			cb(false)
		return
	}
	var userList = {}
	async.waterfall([
		function(next) {
			//获取玩家列表
			DBRemote.dbService.db.hgetall("teaHouseUserInfo:"+teaId,function(err,data) {
				userList = data
				next()
			})
		},
		function() {
			switch(code){
				case 1:
					//获取今天
					var date = utils.getDateString()
					var info = []
					DBRemote.dbService.db.zrevrangebyscore(str+teaId+":"+date,"+inf","-inf","withscores",function(err,data) {
						console.log(data)
						for(var i = 0;i < data.length; i += 2){
							if(data[i] && userList[data[i]] && typeof(userList[data[i]] === "object")){
								info.push({uid : data[i],value : parseInt(data[i + 1]),info : JSON.parse(userList[data[i]])})
							}
						}
						cb(info)
					})
				return
				case -1:
					//获取昨天
					var date = utils.getlackdayString(1)
					var info = []
					DBRemote.dbService.db.zrevrangebyscore(str+teaId+":"+date,"+inf","-inf","withscores",function(err,data) {
						console.log(data)
						for(var i = 0;i < data.length; i += 2){
							if(data[i] && userList[data[i]] && typeof(userList[data[i]] === "object")){
								info.push({uid : data[i],value : parseInt(data[i + 1]),info : JSON.parse(userList[data[i]])})
							}
						}
						cb(info)
					})
				return
				case 7:
					//获取七天
					var info = {}
					var count = 0
					var maxCount = 7
					for(var i = 0;i < maxCount;i++){
						var date = utils.getlackdayString(parseInt(i))
						DBRemote.dbService.db.zrevrangebyscore(str+teaId+":"+date,"+inf","-inf","withscores",function(err,data) {
							if(data){
								for(var i = 0;i < data.length; i += 2){
									if(data[i] && userList[data[i]] && typeof(userList[data[i]] === "object")){
										if(!info[data[i]]){
											info[data[i]] = {uid : data[i],value : 0,info : userList[data[i]] ? JSON.parse(userList[data[i]]) : {}}
										}
										info[data[i]].value += parseInt(data[i + 1])
									}
								}
							}
							count++
							if(count === maxCount){
								console.log(info)
								cb(info)
							}
						})
					}
				return
				case 999:
					var info = []
					DBRemote.dbService.db.zrevrangebyscore(str+teaId+":all","+inf","-inf","withscores",function(err,data) {
						console.log(data)
						for(var i = 0;i < data.length; i += 2){
							if(data[i] && userList[data[i]] && typeof(userList[data[i]] === "object")){
								info.push({uid : data[i],value : parseInt(data[i + 1]),info : JSON.parse(userList[data[i]])})
							}
						}
						cb(info)
					})
				return
				default:
					cb(false)
					return
			}
		}
	],function(err) {
		console.log(err)
	})
}
//离开桌子回调
remote.leaveTeaTableCB = function(uid,roomId,cb) {
	roomId = parseInt(roomId)
	var teaId = parseInt(roomId / 100)
	var tableId = parseInt(roomId % 100)
	var notify = {
		"code" : "userQuitTable",
		"tableId" : tableId,
		"uid" : uid
	}
	local.sendTea(teaId,notify)
	cb()
}
//加入桌子回调
local.joinTeaTableCB = function(uid,roomId) {
	var cmd1 = "nn:acc:"+uid+":"+"uid"
	var cmd2 = "nn:acc:"+uid+":"+"head"
	var cmd3 = "nn:acc:"+uid+":"+"nickname"
	DBRemote.dbService.db.mget(cmd1,cmd2,cmd3,function(err,data) {
		if(!err && data){
			roomId = parseInt(roomId)
			var teaId = parseInt(roomId / 100)
			var tableId = parseInt(roomId % 100)
			var notify = {
				"code" : "userJoinTable",
				"tableId" : tableId,
				"uid" : uid,
				"userInfo" : {uid : data[0],head : data[1],nickname : data[2]}
			}
			local.sendTea(teaId,notify)
		}
	})
}
//获取茶楼桌子信息
remote.getTableInfo = function(uid,cb) {
	var teaId = userTeaMap[uid]
	if(!teaId){
		DBRemote.app.rpc.game.remote.sendTipsByUid(null,uid,"请先加入一个茶楼",function(){})
		cb(false)
		return
	}
    DBRemote.app.rpc.game.remote.getTeaRoomInfo(null,teaId, function(data){
        cb(data)
    })
}

//加入桌子
remote.joinTeaTable = function(uid,sid,tableId,ip,cb){
	var self = this
	var teaId = userTeaMap[uid]
	if(!teaId){
		DBRemote.app.rpc.game.remote.sendTipsByUid(null,uid,"请先加入一个茶楼",function(){})
		cb(false)
		return
	}
	var teaHouseInfo = {}
	var masterUid = 0
    var roomId = parseInt(teaId) * 100 + parseInt(tableId)
	async.waterfall([
		function(next) {
			//获取茶楼信息
			DBRemote.dbService.db.hgetall("teaHouse:"+teaId,function(err,data) {
				if(err || data == null){
					console.log(err)
					cb(false)
					return
				}
				teaHouseInfo = data
				next()
			})
		},
		function(next) {
			masterUid = teaHouseInfo.masterUid
			//检测成员列表
			self.app.rpc.db.club.checkClubLimit(null,masterUid,uid,function(flag) {
				if(flag){
					next()
				}else{
	                cb(false,{"code" : tips.CLUB_LIMIT})
	                return
				}
			})
		},
		function(next) {
			//未开启则创建房间
			if(teaHouseInfo["tableState"+tableId] == false || teaHouseInfo["tableState"+tableId] == "false"){
				var params = JSON.parse(teaHouseInfo["table"+tableId]).param
				if(params){
					self.app.rpc.game.remote.teaRoomCreate(null,uid,sid,masterUid,teaId,tableId,params,function(flag,msg) {
						// console.log("teaRoomCreate : ")
						// console.log(flag)
						// console.log(msg)
						if(flag){
							if(params.consumeMode == conf.MODE_DIAMOND_HOST){
								// console.log("11111")
								next()
							}else{
								// console.log("111112222")
								local.joinTeaTableCB(uid,roomId)
								cb(flag,msg)
							}
						}else{
							if(msg && msg.code == tips.NO_DIAMOND){
								if(params.consumeMode == conf.MODE_DIAMOND_HOST){
									DBRemote.app.rpc.game.remote.sendTipsByUid(null,uid,"楼主钻石不足",function(){})
								}else{
									DBRemote.app.rpc.game.remote.sendTipsByUid(null,uid,"钻石不足",function(){})
								}
							}
							cb(flag,msg)
						}
					})
				}else{
					console.log("params error")
					console.log(params)
					cb(false)
				}
			}else{
				next()
			}
		},
		function() {
			console.log("111113333")
			//加入房间
			var params = {
				"roomId" : roomId,
				"ip" : ip
			}
	        self.app.rpc.game.remote.receive(null, uid, sid,"join",params, function(flag,msg){
	        	if(flag){
	        		local.joinTeaTableCB(uid,roomId)
	        	}else{
					if(msg && msg.code == tips.NO_DIAMOND){
						DBRemote.app.rpc.game.remote.sendTipsByUid(null,uid,"钻石不足",function(){})
					}
	        	}
	            cb(flag,msg)
	        })
		}
		],function(err) {
		console.log(err)
		cb(false)
	})
}
//创建茶楼
remote.createTeaHouse = function(uid,managerFlag,cb) {
	var teaId = 0
	var nickname = ""
	var head = ""
	var teaCfg
	async.waterfall([
			function(next) {
				//判断该玩家是否已有茶楼
				DBRemote.dbService.db.hget("teaHouseList",uid,function(err,data) {
					if(data){
						console.log("玩家已存在茶楼:")
						console.log(data)
						cb(false)
						return
					}else{
						next()
					}
				})
			},
			function(next) {
				DBRemote.dbService.db.incrby("teaHouse:lastid",Math.floor(Math.random() * 20) + 1,function(err,data) {
					console.log("茶楼ID")
					teaId = data
					next()
				})
			},
			function(next) {
				//获取玩家信息
				var cmd1 = "nn:acc:"+uid+":nickname"
				var cmd2 = "nn:acc:"+uid+":head"
				DBRemote.dbService.db.mget(cmd1,cmd2,function(err,data) {
					if(err){
						cb(false)
						return
					}
					nickname = data[0]
					head = data[1]
					next()
				})
			},
			function(next) {
				//获取茶楼配置
				DBRemote.dbService.db.hgetall("config:tea",function(err,data) {
					teaCfg = data
					next()
				})
			},
			function(next) {
				if(managerFlag){
					//管理员创建不消耗
					next()
				}else{
					//判断钻石是否足够
					DBRemote.dbService.getPlayer(uid,"diamond",function(value) {
						console.log(value)
						console.log(teaCfg)
						teaCfg.open = parseInt(teaCfg.open)
						if(value >= teaCfg.open){
							DBRemote.app.rpc.db.remote.setValue(null,uid,"diamond",-teaCfg.open,function(){
								next()
							})
						}else{
							cb(false)
						}
					})
				}
			},
			function(next) {
				//创建茶楼
				var teaInfo = {
					"teaId" : teaId,				//茶楼ID
					"masterUid" : uid,				//主人UID
					"masterNickname" : nickname,    //主人昵称
					"masterHead" : head,			//主人头像
					"teaName" : "茶楼",				//茶楼名称
					"teaNotify" : "",				//茶楼公告
					"openTable" : 1 				//开放的桌子数
				}
				teaInfo["openTable"] = teaCfg["defaultCount"]
				for(var i = 1;i <= teaInfo["openTable"];i++){
					teaInfo["tableState" + i] = false
					teaInfo["table" + i] = JSON.stringify({ 			//桌子参数
						"tableName" : "桌子"+i,
						"tableId" : i,
						"param" : false
					})
				}
				DBRemote.dbService.db.hmset("teaHouse:"+teaId,teaInfo)
				//茶楼列表
				var info = {
					"teaId" : teaId,				//茶楼ID
					"masterUid" : uid,				//主人UID
					"masterNickname" : nickname,    //主人昵称
					"masterHead" : head,			//主人头像
				}
				DBRemote.dbService.db.hset("teaHouseList",uid,JSON.stringify(info))
				cb(true,teaId)
			}
		],
		function(err) {
			console.log(err)
		})
}

//关闭茶楼
remote.closeTeaHouse = function(uid,cb) {
	DBRemote.dbService.db.hget("teaHouseList",uid,function(err,data) {
		if(data){
			data = JSON.parse(data)
			var teaId = data.teaId
			DBRemote.dbService.db.del("teaHouse:"+teaId,function(err,data) {if(err){console.log(err)}})
			DBRemote.dbService.db.del("teaHousePlayCount:"+teaId,function(err,data) {if(err){console.log(err)}})
			DBRemote.dbService.db.del("teaHouseRankingDate:"+teaId,function(err,data) {if(err){console.log(err)}})
			DBRemote.dbService.db.del("teaHouseRecord:"+teaId,function(err,data) {if(err){console.log(err)}})
			DBRemote.dbService.db.del("teaHouseScores"+teaId+"all",function(err,data) {if(err){console.log(err)}})
			DBRemote.dbService.db.del("teaHouseTableCount:"+teaId,function(err,data) {if(err){console.log(err)}})
			DBRemote.dbService.db.del("teaHouseUserInfo:"+teaId,function(err,data) {if(err){console.log(err)}})
			DBRemote.dbService.db.del("teaHouseWinCount:"+teaId,function(err,data) {if(err){console.log(err)}})
			DBRemote.dbService.db.hdel("teaHouseList",uid,function(err,data) {if(err){console.log(err)}})
			cb(true)
		}else{
			cb(false)
		}
	})
}

//获取茶楼列表
remote.getTeaList = function(cb) {
	DBRemote.dbService.db.hgetall("teaHouseList",function(err,data) {
		if(data){
			for(var i in data){
				data[i] = JSON.parse(data[i])
			}
			cb(true,data)
		}else{
			cb(true,{})
		}
	})
}

//获取我的茶楼
remote.getMyTeaHouse = function(uid,cb) {
	DBRemote.dbService.db.hget("teaHouseList",uid,function(err,data) {
		if(err || !data){
			console.log(err)
			console.log(data)
			cb(false)
			return
		}
		cb(true,JSON.parse(data))
	})
}

//加入茶楼
remote.joinTeaHouse = function(uid,sid,teaId,cb) {
	DBRemote.dbService.db.hgetall("teaHouse:"+teaId,function(err,data) {
		if(err || !data){
			cb(false)
			return
		}
		for(var i = 1; i <= data.openTable;i++){
			data["table" + i] = JSON.parse(data["table" + i])
		}
		local.joinTea(teaId,uid)
		remote.getTableInfo(uid,function(tableInfo) {
			var info = {
				teaInfo : data,
				tableInfo : tableInfo
			}
			cb(info)
		})
	})
}
//离开茶楼
remote.leaveTeaHouse = function(uid,cb) {
	local.leaveTea(uid)
	if(cb){
		cb()
	}
}

//设置桌子配置
remote.setTeaTableInfo = function(uid,tableId,info,cb) {
	var teaId = 0
	remote.getMyTeaHouse(uid,function(flag,data) {
		if(flag){
			teaId = data.teaId
			DBRemote.dbService.db.hgetall("teaHouse:"+teaId,function(err,data) {
				if(err || !data){
					console.log("获取茶楼信息错误")
					cb(false)
					return
				}
				if(data["tableState"+tableId] == true || data["tableState"+tableId] == "true"){
					DBRemote.app.rpc.game.remote.sendTipsByUid(null,uid,"该桌子正在游戏中,请先解散",function(){})
					cb(false)
					return
				}
				for(var i = 1; i <= parseInt(data.openTable);i++){
					data["table"+i] = JSON.parse(data["table"+i])
				}
				console.log("info")
				console.log(info)
				if(!info || typeof(info) !== "object"){
					data["table"+tableId].param = false
				}else{
					data["table"+tableId].param = info
				}
				console.log(data["table"+tableId])
				var notify = {
					"code" : "updateTeaInfo",
					"data" : data
				}
				local.sendTea(teaId,notify)
				DBRemote.dbService.db.hset("teaHouse:"+teaId,"table"+tableId,JSON.stringify(data["table"+tableId]),function(err,data) {
					cb(true)
				})
			})
		}else{
			console.log("玩家没有茶楼 : "+uid)
			cb(false)
		}
	})
}
//开启桌子
remote.openTable = function(uid,cb) {
	var teaId = 0
	var tableData = {}
	var openTable = 0
	async.waterfall([
			function(next) {
				remote.getMyTeaHouse(uid,function(flag,tmpData) {
					teaId = tmpData.teaId
					if(flag){
						next()
					}else{
						console.log("玩家没有茶楼 : "+uid)
						cb(false)
					}
				})
			},
			function(next) {
				DBRemote.dbService.db.hgetall("teaHouse:"+teaId,function(err,data) {
					if(err || !data){
						console.log("获取茶楼信息错误")
						cb(false)
						return
					}
					tableData = data
					openTable = parseInt(data.openTable)
					if(openTable >= 8){
						console.log("茶楼已达上限")
						cb(false)
						return
					}
					openTable++
					next()
				})
			},
			function(next) {
				//获取茶楼配置
				DBRemote.dbService.db.hgetall("config:tea",function(err,data) {
					teaCfg = data
					next()
				})
			},
			function(next) {
				//判断钻石是否足够
				DBRemote.dbService.getPlayer(uid,"diamond",function(value) {
					teaCfg["table"+openTable] = parseInt(teaCfg["table"+openTable])
					if(value >= teaCfg["table"+openTable]){
						DBRemote.app.rpc.db.remote.setValue(null,uid,"diamond",-teaCfg["table"+openTable],function(){
							next()
						})
					}else{
						DBRemote.app.rpc.game.remote.sendTipsByUid(null,uid,"钻石不足",function(){})
						cb(false)
					}
				})
			},
			function() {
				for(var i = 1; i <= parseInt(tableData.openTable);i++){
					tableData["table"+i] = JSON.parse(tableData["table"+i])
				}
				var info = {
					openTable : openTable
				}
				var tableInfo = {
					"tableName" : "桌子"+openTable,
					"tableId" : openTable,
					"param" : false
				}
				info["table"+openTable] =  JSON.stringify(tableInfo)
				info["tableState"+openTable] = false
				tableData["table"+openTable] = tableInfo
				tableData["openTable"] = openTable
				console.log(info)
				DBRemote.dbService.db.hmset("teaHouse:"+teaId,info)
				var notify = {
					"code" : "updateTeaInfo",
					"data" : tableData
				}
				local.sendTea(teaId,notify)
				cb(true)
			}
		],function(err) {
		console.log(err)
	})
}
//解散桌子
remote.dissolved = function(uid,tableId,cb){
	remote.getMyTeaHouse(uid,function(flag,data) {
		if(flag){
			teaId = data.teaId
			var roomId = parseInt(teaId) * 100 + parseInt(tableId)
			DBRemote.app.rpc.game.remote.finishRoom(null,roomId,function(flag) {
				if(flag){
					var notify = {
						"code" : "dissolved",
						"tableId" : tableId
					}
					local.sendTea(teaId,notify)
				}
				cb(flag)
			})
		}else{
			console.log("玩家没有茶楼 : "+uid)
			cb(false)
		}
	})
	
}
//获取茶楼配置
remote.getTeaCfg = function(uid,cb) {
	DBRemote.dbService.db.hgetall("config:tea",function(err,data) {
		cb(true,data)
	})
}

//设置茶楼配置
remote.setTeaCfg = function(cfg,cb) {
	DBRemote.dbService.db.hgetall("config:tea",function(err,data) {
		for(var i in data){
			if(typeof(cfg[i]) == "number" && cfg[i] >= 0){
				data[i] = cfg[i]
			}
		}
		DBRemote.dbService.db.hmset("config:tea",data,function(err) {
			if(err){
				console.log(err)
			}
			cb(true,data)
		})
	})
}
//设置茶楼公告、名字
remote.setTeaInfo = function(uid,name,notify,cb) {
	if(!name || typeof(name) !== "string" || name.length > 12 || typeof(notify) !== "string" || notify.length > 200){
		cb(false)
		return
	}
	remote.getMyTeaHouse(uid,function(flag,data) {
		if(flag){
			teaId = data.teaId
			DBRemote.dbService.db.hset("teaHouse:"+teaId,"teaName",name)
			DBRemote.dbService.db.hset("teaHouse:"+teaId,"teaNotify",notify)
			local.sendTea(teaId,{"code" : "teaInfo","name" : name,"notify" : notify})
			cb(true)
		}else{
			console.log("玩家没有茶楼 : "+uid)
			cb(false)
		}
	})
}

//设置楼主自定义数据
remote.setTeaDefined = function(uid,obj,cb) {
	remote.getMyTeaHouse(uid,function(flag,data) {
		if(flag){
			var str = JSON.stringify(obj)
			if(str.length > 3000){
				console.log("str.length : "+str.length)
				cb(false)
				return
			}
			teaId = data.teaId
			DBRemote.dbService.db.hset("teaHouse:"+teaId,"defined",str)
			local.sendTea(teaId,{"code" : "updateDefined","obj" : obj})
			cb(true)
		}else{
			console.log("玩家没有茶楼 : "+uid)
			cb(false)
		}
	})
}

 //获取经营数据
 remote.getRunData = function(uid,cb) {
	remote.getMyTeaHouse(uid,function(flag,data) {
		if(flag){
			teaId = data.teaId
			var notify = {
				"teaHouseDiamod" : {},
				"teaHouseTableCount" : {}
			}
			DBRemote.dbService.db.hgetall("teaHouseDiamod:"+teaId,function(err,data1) {
				notify.teaHouseDiamod = data1
				DBRemote.dbService.db.hgetall("teaHouseTableCount:"+teaId,function(err,data2) {
					notify.teaHouseTableCount = data2
					cb(true,notify)
				})
			})
		}else{
			console.log("玩家没有茶楼 : "+uid)
			cb(false)
		}
	})
 }

local.joinTea = function(teaId,uid) {
	console.log("joinTea")
	//离开之前的茶楼
	local.leaveTea(uid)
	//加入新茶楼
	if(!teaUserMap[teaId]){
		teaUserMap[teaId] = {}
	}
	teaUserMap[teaId][uid] = true
	userTeaMap[uid] = teaId
}

local.leaveTea = function(uid) {
	var teaId = userTeaMap[uid]
	if(teaId){
		delete userTeaMap[uid]
		if(teaUserMap[teaId]){
			delete teaUserMap[teaId][uid]
		}
	}
}
//发送茶楼信息
local.sendTea = function(teaId,notify) {
	notify.cmd = "teaMessage"
	for(var uid in teaUserMap[teaId]){
		DBRemote.app.rpc.game.remote.sendByUid(null,uid,notify,function(){})
	}
}

local.getDateString = function() {
	var myDate = new Date()
	var month = myDate.getMonth() + 1
	var date = myDate.getDate()
	if(month < 10){
		month = "0"+month
	}
	if(date < 10){
		date = "0"+date
	}
	var dateString = parseInt(""+myDate.getFullYear() + month + date)
	return dateString
}